package com.vogue.supplymall.admin.business.common;

import com.vogue.supplymall.common.domain.dto.admin.OrganManagementInfo;
import com.vogue.supplymall.common.domain.dto.admin.PrivatePlatformInfo;
import com.vogue.supplymall.common.exception.FormatIncorrectException;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * The type Open organ check utils.
 * 开通店铺-输入内容check
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-05-03
 */
public class OpenOrganCheckUtils {

    /**
     * Apply boolean.
     *
     * @param managementInfo the management info
     * @return the boolean
     */
    public static Boolean apply(OrganManagementInfo managementInfo) {
        // 入住平台必须选择
        if (managementInfo.getPlatformInfo().size() == 0) {
            return true;
        }
        // 计费方式的check
        if (managementInfo.getBillingtype() == 3) {
            if (StringUtils.isEmpty(managementInfo.getSingleratio()) || StringUtils.isEmpty(managementInfo.getUpperlimit())) {
                return true;
            }
        } else {
            if (StringUtils.isEmpty(managementInfo.getCost())) {
                return true;
            }
        }
        return null == managementInfo.getBillingtype();
    }

    /**
     * Check opend organ list boolean.
     *
     * @param managementInfo the management info
     * @return the boolean
     */
    public static Boolean checkOpendOrganList(OrganManagementInfo managementInfo) {
        if (StringUtils.isNotEmpty(managementInfo.getSysCode())) {
            return isPureDigital(managementInfo.getSysCode().toString(), "注册码");
        }
        return false;
    }

    /**
     * Check add private.
     *
     * @param privatePlatformInfo the private platform info
     */
    public static void checkAddPrivate(PrivatePlatformInfo privatePlatformInfo) {
        checkPlatform(privatePlatformInfo);
        checkFileName(privatePlatformInfo);
        checkHttp(privatePlatformInfo);
    }

    /**
     * @param privatePlatformInfo
     * @return
     */
    private static Boolean checkFileName(PrivatePlatformInfo privatePlatformInfo) {
        if (StringUtils.isNotEmpty(privatePlatformInfo.getFilename())) {
            return isFileName(privatePlatformInfo.getFilename(), "文件名称");
        }
        return false;
    }

    /**
     * @param privatePlatformInfo
     * @return
     */
    private static Boolean checkPlatform(PrivatePlatformInfo privatePlatformInfo) {
        if (StringUtils.isNotEmpty(privatePlatformInfo.getPlatform())) {
            return isPureDigital(privatePlatformInfo.getPlatform(), "内部码");
        }
        return false;
    }

    /**
     * @param privatePlatformInfo
     * @return
     */
    private static Boolean checkHttp(PrivatePlatformInfo privatePlatformInfo) {
        if (StringUtils.isNotEmpty(privatePlatformInfo.getUrl())) {
            return isHttp(privatePlatformInfo.getUrl(), "下载网址");
        }
        return false;
    }

    /**
     * @param string
     * @param attrName
     * @return
     */
    private static boolean isHttp(String string, String attrName) {
        String regEx1 = "^((https|http){1}(:\\/\\/){1}).+$";
        Pattern p = Pattern.compile(regEx1);
        Matcher m = p.matcher(string);
        if (!m.matches()) {
            throw new FormatIncorrectException(attrName);
        }
        return true;
    }

    /**
     * @param string
     * @param attrName
     * @return
     */
    private static boolean isFileName(String string, String attrName) {
        String regEx1 = "^[A-Za-z0-9\\-]+$";
        Pattern p = Pattern.compile(regEx1);
        Matcher m = p.matcher(string);
        if (!m.matches()) {
            throw new FormatIncorrectException(attrName);
        }
        return true;
    }

    /**
     * @param string
     * @param attrName
     * @return
     */
    private static boolean isPureDigital(String string, String attrName) {
        String regEx1 = "\\d+";
        Pattern p = Pattern.compile(regEx1);
        Matcher m = p.matcher(string);
        if (!m.matches()) {
            throw new FormatIncorrectException(attrName);
        }
        return true;
    }
}
